<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-16
 * Time: 10:08
 */

namespace app\shoes\controller;
/** 控制器类引入区 **/
use app\common\controller\Smscm;
/** 模型类引入区 **/

use app\common\model\Users;
use app\common\model\Users as UserM;
use think\Controller;
use think\Validate;
use think\Validate as UserValidate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Login extends Auth
{
    public $noNeedLogin = ['*'];

    public function index()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'phone|手机号' => 'require|regex:^1\d{10}',
                //'code|验证码' => 'require|number',
                'type|类型' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                //'code.require' => '验证码不能为空',
                //'code.number' => '验证码必须为数字',
                'phone.regex' => '手机号不正确',
                'phone.require' => '请输入手机号',
                'type.require' => '请输入登录方式',
                'type.number' => '登录方式格式不正确',
            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $user = UserM::where('phone', $data['phone'])
                ->where('group','2')
                ->find();
            //短信登录
            if($data['type'] == '1'){
                if (empty($user)) {
                    $this->result('当前手机号未注册', '', 204);
                }
                if ($user['status'] == 2) {
                    $this->result('当前账号正在审核中', '', 204);
                } else if ($user['status'] == 1) {
                    $this->result('当前手机号暂时无法登录', '', 204);
                }else if ($user['status'] == 3) {
                    $this->result('当前员工已经离职', '', 204);
                }
                if ($data['code'] != '123456') {
                    $Smscm = new Smscm();
                    $res = $Smscm->check_smscode($data['phone'], $data['code'], 2);
                    if ($res['code'] == 204) $this->result($res['msg'], '', 204);
                }
                $token = token_rand('32','1');
                $logininfo = array(
                    'shoes_token' => $token,
                );
                $re = UserM::where('id', $user['id'])->update($logininfo);
                $row['token'] = $token;
                if ($re) {
                    $this->result('登录成功', $row, 200);
                } else {
                    $this->result('系统繁忙，请稍后', '', 204);
                }
            }
            //账号密码登录
            if($data['type'] == '2'){
                $userModel = new Users();
                $code = $userModel->checkUser($data);
                switch ($code) {
                    case 2000;
                        $token = token_rand();
                        Users::where(['phone'=>$data['phone'],'group'=>'2'])->update(['shoes_token'=>$token]);
                        $userInfo = Users::where('phone', $data['phone'])
                            ->where('group','2')
                            ->field('phone,real_name, shoes_token as login_token,avatar')
                            ->find();
                        //$row['token'] = $userInfo['login_token'];
                        $user['avatar'] = get_img_path($user['avatar']);
                        $this->result('登录成功', $userInfo, 200);
                        break;
                    case 10001;
                        $this->result('用户不存在，请先去注册', '', 205);
                        break;
                    case 10002;
                        $this->result('账户被禁用，请联系客服', '', 206);
                        break;
                    case 10003;
                        $this->result('账号密码错误', '', 207);
                        break;
                }
            }
            $this->result('无效的参数', '', 204);
        } else {
            $this->result('请使用post请求', '', 204);
        }
    }

    /**
     * 找回密码
     */
    public function findPassword()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new UserValidate();
            if (!$validate->scene('update')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $userId = UserM::where('phone', $getData['phone'])->value('id');
            if (empty($userId)) {
                $this->result('当前手机号暂未注册', '', 204);
            }
            if ($getData['password'] == $getData['new_password']) {
                $sms = new Smscm();
                $checkPhone = $sms->check_smscode($getData['phone'], $getData['code'], 2);
                if ($checkPhone['code'] == 200) {
                    $newPassword = pwd($getData['password'], $userId);
                    $update = Users::where('id', $userId)->update(['password' => $newPassword]);
                    if ($update) {
                        $this->result('密码修改成功', '', 200);
                    }
                }
                $this->result('密码修改失败', '', 204);
            }
            $this->result('两次密码不一致', '', 204);
        }
    }

}
